package com.ylx.service;

import com.ylx.mapper.UserMapper;
import com.ylx.pojo.User;
import com.ylx.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class UserServiceIpml implements UserService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public User select (String username, String password, String role) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.select(username, password,role);
        sqlSession.close();
        return user;
    }

    @Override
    public User select1 (String username, String password) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.select1(username,password);
        sqlSession.close();
        return user;
    }

    @Override
    public User selectByUsername (String username) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.selcetByUsername(username);
        sqlSession.close();
        return user;
    }

    @Override
    public void add (User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.add(user);
        sqlSession.commit();
        sqlSession.close();

    }

    @Override
    public int updateZp (User user) {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int i = userMapper.updateZp(user);
        sqlSession.commit();
        sqlSession.close();

        return i;
    }

    @Override
    public    User selectUserByUid(String uid) {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.selectUserByUid(uid);
        sqlSession.close();
        return user;

    }
}
